package com.mycase.im.boot.controller;

import com.mycase.im.boot.entity.database.ImChatGroup;
import com.mycase.im.boot.entity.database.ImChatGroupUser;
import com.mycase.im.boot.exception.CustomException;
import com.mycase.im.boot.service.ChatGroupService;
import com.mycase.im.boot.service.ChatGroupUserService;
import com.mycase.im.boot.service.MessageService;
import com.mycase.im.boot.service.UserService;
import com.mycase.im.boot.entity.communication.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ZhuHJay
 * @Date 2021/9/3 11:07
 */
@Slf4j
@RestController
@RequestMapping("/api/group")
public class ChatGroupController {

    @Autowired
    private ChatGroupService chatGroupService;
    @Autowired
    private ChatGroupUserService chatGroupUserService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public ResponseMessage getGroupList(
            @NotBlank(message = "请求失败")String id){

        // 通过用户的id 去ImChatGroupUser 表获取 该用户 绑定的所有群聊
        List<ImChatGroupUser> imChatGroupUsers = chatGroupUserService.getGroupListByUserId(id);

        // 通过查询到的 群聊id 来获取该群聊的完整信息
        List<ImChatGroup> imChatGroups = new ArrayList<>();
        // 获取所有的群聊列表
        imChatGroupUsers.forEach(item -> {
            ImChatGroup byId = chatGroupService.getById(item.getChatGroupId());
            // 搜索该群聊的所有联系人, 并获取所有联系人的详细信息
            List<ImChatGroupUser> groupUserList = chatGroupUserService.getGroupUserListById(item.getChatGroupId());
            byId.setGroupUsers(groupUserList);
            // 将该群信息放到集合中进行存储
            imChatGroups.add(byId);
        });

        // 通过群的完整信息, 获取群聊相关的所有聊天记录
        imChatGroups.forEach(group ->
            messageService.getGroupMessageRecord(id, group) );

        return ResponseMessage.success().appendData("chatGroupList", imChatGroups);
    }

    @PostMapping("/created")
    public ResponseMessage createGroup(
            @NotBlank(message = "请求失败")String id,
            @NotBlank(message = "群名设置不能为空")String groupName,
            @RequestParam(value = "group", required = false) ArrayList<String> group){

        // 创建群:   1. 进行聊天室的创建
        //          2. 将其余的人拉进聊天室
        // 先进行 group 中判断是否存在
        if( group != null ){
            group.forEach(userId -> {
                if( !userService.isExist(userId) ){
                    throw new CustomException("有不存在的用户, 请重试");
                }
            });
        }else{
            group = new ArrayList<>();
        }
        // 创建聊天室
        ImChatGroup imChatGroup = chatGroupService.createGroup(id, groupName);

        // 不管拉不拉人, 这一步要将创建人添加到群联系人中, 并且发送群创建的消息
        // 当然, 如果有拉人的话, 那么也是一样添加联系人以及创建消息发送
        chatGroupUserService.refreshUsersToGroup(imChatGroup, group);
        return ResponseMessage.success().appendData("groupId", imChatGroup.getId());
    }

    @PostMapping("/insert")
    public ResponseMessage insertUsers(
            @NotBlank(message = "请求失败")String id,
            @NotBlank(message = "请求失败")String groupId,
            @NotNull(message = "邀请人不能为空")@RequestParam(value = "insert") ArrayList<String> insertUsers){

        // 判断群聊是否存在
        ImChatGroup imChatGroup = chatGroupService.getById(groupId);
        if( imChatGroup == null ){
            throw new CustomException("不存在群聊, 请重试");
        }

        // 进行判断列表中的人是否都是存在的
        insertUsers.forEach(userId -> {
            if( !userService.isExist(userId) ){
                throw new CustomException("有不存在的用户, 请重试");
            }
        });

        // 将邀请人加入到群聊中
        chatGroupUserService.inviteUsersToGroup(id, imChatGroup, insertUsers);
        return ResponseMessage.success();
    }

}
